/**
 * 发票选择
 */
import { useState } from 'react';
import { Button, Drawer } from 'antd';
import List from './list';
import BatchUpload from '@/pages/private-file/batch-upload';


import { CheckOutlined, RollbackOutlined } from '@ant-design/icons';
import { getValidateMessage } from '@/utils/validate-rules/message';

interface TaxSelectProps {
  type?: string;
  visible?: boolean;
  cancel: () => void;
  confirm: (values: any[]) => void;
  match: any;
  history: any;
  location: any;
  defaultValue?: any[];
  extraPrams: any;
  rowKey?: string;
}

const TaxSelect = (props: TaxSelectProps) => {
  const { type, visible = false, cancel, confirm, history, match, location, defaultValue, extraPrams, pageCode, pageUrl, rowKey } = props;
  const [importVisible, setImportVisible] = useState<boolean>(false);
  const [values, setValues] = useState<any[]>([]);
  const [refresh, setRefresh] = useState<number>(0);
  const handleCheckClick = () => {
    confirm(values);
  };

  /**
   * 取消选择发票 或者 取消导入发票
   */
  const handleRollbackClick = () => {
    cancel();
  };

  const changeSelect = (vals: any[]) => {
    setValues(vals);
  };

  /**
   * 点击导入按钮
   */
  const importExcel = () => {
    setImportVisible(true);
  };

  const editCbk = () => {
    setRefresh(Math.random());
    importCancel();
  };

  const importCancel = () => {
    setImportVisible(false);
  };


  return (
    <>
      <Drawer
        title={getValidateMessage('fapiao.select.title', true)}
        placement="right"
        closable={false}
        onClose={handleRollbackClick}
        visible={visible}
        width="90%"
        maskClosable={false}
        footer={
          <div className="page__foot">
            <div className="content__slot__left">
              <Button
                onClick={handleRollbackClick}
                icon={<RollbackOutlined />}
                className="reset-btn"
              >
                {getValidateMessage('common.cancel', true)}
              </Button>
            </div>
            <div className="page__foot_right_btns">
              <Button onClick={handleCheckClick} type="primary" icon={<CheckOutlined />}>
                {getValidateMessage('common.confirm', true)}
              </Button>
            </div>
          </div>
        }
      >
        <List
          history={history}
          location={location}
          match={match}
          type={type}
          pageUrl={pageUrl}
          pageCode={pageCode}
          changeSelect={changeSelect}
          importExcel={importExcel}
          refresh={refresh}
          defaultValue={defaultValue}
          extraPrams={extraPrams}
          rowKey={rowKey || 'id'}
        />
      </Drawer>

      {
        importVisible && <Drawer
              title={getValidateMessage('fapiao.select.import', true)}
              placement="right"
              closable={false}
              onClose={importCancel}
              visible={importVisible}
              width="90%"
              maskClosable={false}
          >
            <BatchUpload submitCbk={editCbk} cancel={importCancel} acctCo={extraPrams?.acctCo}/>
          </Drawer>
      }

    </>
  );
};
export default TaxSelect;
